﻿using Code;
using FluorineFx.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Serialize
{
    /// <summary>
    /// 序列化 代码
    /// </summary>
   public  class SerializeCode
    {
        /// <summary>
        /// 循环次数
        /// </summary>
        private static int forInt = 100000;

        /// <summary>
        /// NET系统类 序列化 实体类
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string DataContractJsonModel<T>(T model)
        {
            string JsonStr = "";
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                JsonStr = JsonHelp.ObjectToJson(model);
            }
            timer.Stop();
            Console.WriteLine("大小:" + StringToSize(JsonStr) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return JsonStr;
        }

        /// <summary>
        /// NET系统类 序列化 实体类 加速
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string DataContractJsonModelSpeed<T>(T model)
        {
            string JsonStr = "";
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                JsonStr = JsonHelp.ModelToJson(model);
            }
            timer.Stop();
            Console.WriteLine("大小:" + StringToSize(JsonStr) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return JsonStr;
        }


        /// <summary>
        /// NET WebJavaScript系统类 序列化 实体类 加速
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string WebJavaScriptJsonModelSpeed<T>(T model)
        {
            string JsonStr = "";
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                JsonStr = JsonHelp.ModelToJavaScriptJson(model);
            }
            timer.Stop();
            Console.WriteLine("大小:" + StringToSize(JsonStr) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return JsonStr;
        }

        /// <summary>
        /// Newtonsoft.Json 序列化 实体类
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string NewtonsoftJsonModel<T>(T model)
        {
            string JsonStr = "";
            Newtonsoft.Json.JsonSerializerSettings jsetting = new Newtonsoft.Json.JsonSerializerSettings()
            {
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
                Formatting = Newtonsoft.Json.Formatting.None,
            };
            jsetting.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss" });

            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                JsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(model, jsetting);
            }
            timer.Stop();
            Console.WriteLine("大小:" + StringToSize(JsonStr) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return JsonStr;
        }



        /// <summary>
        /// Newtonsoft.Json 序列化 Dictionary
        /// </summary>
        /// <returns></returns>
        public static string NewtonsoftJsonDictionary<T>(T jsonObj)
        {
            string JsonStr = "";
            Newtonsoft.Json.JsonSerializerSettings jsetting = new Newtonsoft.Json.JsonSerializerSettings()
            {
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
                Formatting = Newtonsoft.Json.Formatting.None,
            };
            jsetting.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss" });

            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                JsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, jsetting);
            }
            timer.Stop();
            Console.WriteLine("大小:" + StringToSize(JsonStr) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return JsonStr;
        }


        /// <summary>
        /// FluorineFx 序列化 实体类
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <param name="timer"></param>
        /// <param name="model"></param>
        public static void FluorineFxJsonModel<T>(T model)
        {
            string JsonStr = "";

            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                JsonStr = JavaScriptConvert.SerializeObject(model);
                //JsonStr = Regex.Replace(JsonStr, @"new Date\((\d+)\)", match =>
                //{
                //    DateTime dt = new DateTime(1970, 1, 1);
                //    dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                //    dt = dt.ToLocalTime();
                //    return dt.ToString("yyyy-MM-ddTHH:mm:ss");
                //});
            }
            timer.Stop();
            Console.WriteLine("大小:" + StringToSize(JsonStr) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
        }

        

        /// <summary>
        /// FluorineFx 序列化 Dictionary
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <param name="timer"></param>
        /// <param name="model"></param>
        public static string FluorineFxJsonDictionary(Dictionary<string, object> jsonObj)
        {
            string JsonStr = "";
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                JsonStr = JavaScriptConvert.SerializeObject(jsonObj);
            }
            timer.Stop();
            Console.WriteLine("大小:" + StringToSize(JsonStr) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return JsonStr;
        }

       
        
        /// <summary>
        /// FluorineFx 二进制 序列化  Object
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <param name="timer"></param>
        /// <param name="model"></param>
        public static byte[] FluorineFxAMF3Object(object jsonObj)
        {
            string JsonStr = "";
            byte[] buf = null;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                using (System.IO.MemoryStream steram = new System.IO.MemoryStream())
                {
                    FluorineFx.AMF3.ByteArray ba = new FluorineFx.AMF3.ByteArray(steram);
                    ba.WriteObject(jsonObj);
                    buf=steram.ToArray();
                }
            }
            timer.Stop();
            JsonStr = Convert.ToBase64String(buf);
            Console.WriteLine("大小:" + Help.BytesToSize(buf) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return buf;
        }

        /// <summary>
        /// FluorineFx 二进制 序列化  Bytes
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <param name="timer"></param>
        /// <param name="model"></param>
        public static byte[] FluorineFxAMF3Bytes(Byte[] jsonByte)
        {
            string JsonStr = "";
            byte[] buf = null;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                using (System.IO.MemoryStream steram = new System.IO.MemoryStream())
                {
                    FluorineFx.AMF3.ByteArray ba = new FluorineFx.AMF3.ByteArray(steram);
                    ba.WriteBytes(jsonByte, 0, jsonByte.Length);
                    buf = steram.ToArray();
                }
            }
            timer.Stop();
            JsonStr = Convert.ToBase64String(buf);
            Console.WriteLine("大小:" + Help.BytesToSize(buf) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return buf;
        }

        /// <summary>
        /// FluorineFx 二进制 序列化  String
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <param name="timer"></param>
        /// <param name="model"></param>
        public static byte[] FluorineFxAMF3String(string json)
        {
            string JsonStr = "";
            byte[] buf = null;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                using (System.IO.MemoryStream steram = new System.IO.MemoryStream())
                {
                    FluorineFx.AMF3.ByteArray ba = new FluorineFx.AMF3.ByteArray(steram);
                    ba.WriteUTF(json);
                    buf = steram.ToArray();
                }
            }
            timer.Stop();
            JsonStr = Convert.ToBase64String(buf);
            Console.WriteLine("大小:" + Help.BytesToSize(buf) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return buf;
        }

        /// <summary>
        /// BinaryFormatter 二进制 序列化 
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <param name="timer"></param>
        /// <param name="model"></param>
        public static byte[] BinaryFormatter(object jsonObj)
        {
            string JsonStr = "";
            byte[] buf = null;
            System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                using (System.IO.MemoryStream steram = new System.IO.MemoryStream())
                {
                    formatter.Serialize(steram, jsonObj);
                    buf = steram.ToArray();
                }
            }
            timer.Stop();
            JsonStr = Convert.ToBase64String(buf);
            Console.WriteLine("大小:" + Help.BytesToSize(buf) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return buf;
        }


        /// <summary>
        /// FluorineFx 二进制 反序列化 Dictionary
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <param name="timer"></param>
        /// <param name="model"></param>
        public static Dictionary<string, object> FluorineFxDictionaryAMF3(string JosnBase64 = "")
        {
            string JsonStr = "";
            Dictionary<string, object> jsonObj = null;
             JosnBase64=JosnBase64!=""?JosnBase64:"CgsBCU5hbWUGGeS4gOe6p+iKgueCuRFDaGlsZHJlbgkDAQoLAQAGFU7nuqfoioLngrkECQMBCgsBAAYN5rKQ5YWJB0FnZQQZEVBhc3NXb3JkBkFFMTBBREMzOTQ5QkE1OUFCQkU1NkUwNTdGMjBGODgzRRVDcmVhdGVUaW1lBicyMDE2LTAyLTIyVDEwOjI1OjM1C0lzRW5kAwEBAQ==";
            byte[] buf = Convert.FromBase64String(JosnBase64);

            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                using (System.IO.MemoryStream steram = new System.IO.MemoryStream(buf))
                {
                    FluorineFx.AMF3.ByteArray ba = new FluorineFx.AMF3.ByteArray(steram);
                    jsonObj = ba.ReadObject() as Dictionary<string, object>;
                }
            }
            timer.Stop();
            JsonStr = JavaScriptConvert.SerializeObject(jsonObj);
            Console.WriteLine("大小:" + Help.BytesToSize(buf) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return jsonObj;
        }

        /// <summary>
        /// 计算 存储单位
        /// </summary>
        /// <param name="len">
        public static string StringToSize(string str)
        {
            Byte[] byteArray = Encoding.UTF8.GetBytes(str);
            string base64 = Convert.ToBase64String(byteArray);
            return Help.BytesToSize(byteArray);
        }


        
    }

}
